import { connect, useStore, useLocation } from "umi";
// 需要type
const Access = (props) => {
  const { permissions, children, type, customPer } = props;
  const { pathname } = useLocation();
  const arr = pathname.split("/");
  let _per = "";
  for (let i = 1; i < arr.length; i++) {
    _per = _per + `${arr[i]}:`;
  }
  _per = _per + type;

  if (permissions.includes("*:*:*")) return children; // admin
  if (permissions.includes(customPer)) return children; // 单独定义权限
  if (permissions.includes(_per)) return children;
};

export default connect(({ global }) => ({
  ...global,
}))(Access);

export const useAccess = (customPer) => {
  const { permissions } = useStore().getState().global;
  if (permissions.includes("*:*:*")) return true;
  if (permissions.includes(customPer)) return true; // 单独定义权限
};
